package com.yyds.yygh.cmn.controller;

import com.yyds.yygh.cmn.service.DictService;
import com.yyds.yygh.common.result.Result;
import com.yyds.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Api(tags = "数据字典接口")
@RestController
@RequestMapping(value = "/admin/cmn/dict")
public class DictController {
    @Autowired
    private DictService dictService;

    //根据数据id查询子数据列表
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping(value = "/findChildData/{parentId}")
    public Result findChildData(@PathVariable Long parentId){
        List<Dict> dictList = dictService.findChlidData(parentId);
        return Result.success(dictList);
    }

    @ApiOperation(value = "导出数据")
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse httpServletResponse){
        dictService.exportData(httpServletResponse);
    }
    @ApiOperation(("导入数据"))
    @PostMapping(value = "/importData")
    public Result importData(MultipartFile file){
        dictService.importDictData(file);
        return Result.success();
    }
    @ApiOperation("获取数据字典名称")
    @GetMapping("/getName/{parentDictCode}/{value}")
    public String getName(@PathVariable String parentDictCode,
                          @PathVariable String value){
        String name = dictService.getNameByParentDictCodeAndValue(parentDictCode, value);
        return name;
    }
    @ApiOperation("获取数据字典名称")
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable String value){
        String name = dictService.getNameByParentDictCodeAndValue("",value);
        return name;
    }
    @ApiOperation(("根据dictCode获取下级字典"))
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode){
        List<Dict> dict = dictService.findByDictCode(dictCode);
        return Result.success(dict);
    }
}
